export const createForm = (params = {}) => ({
  themeName: '', // 主题名称
  summary: '', // 主题概述
  hitLocation: '', // 命中位置
  region: params.region || '', // 内容地域
  exclude: '', // 排除关键词
  includes: [['', '']], // 关键词组
  endTime: '',
  startTime: '',
})

export const createRules = () => ({
  themeName: [{ required: true, message: '主题名称不能为空', trigger: 'blur' }],
  summary: [{ required: true, message: '主题概述不能为空', trigger: 'blur' }],
  includes: { required: true, validator: validateIncldes, message: '关键词组不能为空', trigger: 'blur' },
  startTime: [{ required: true, validator: validate, message: '监测周期不能为空', trigger: 'blur' }],
  exclude: { required: false, message: '排除关键词不能为空', trigger: 'blur' },
})

const validate = (rule, value, callback) => {
  if (!value && value !== 0) {
    callback(new Error(rule.message))
  } else {
    callback()
  }
}

const validateIncldes = (rule, value, callback) => {
  for (let i = 0, len = value.length; i < len; i++) {
    const empty = value[i].every((it) => !it.trim())
    if (empty) {
      callback(new Error('关键词组不可都为空'))
    }
  }
  callback()
}
